package net.hands.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
/**
 * 
 * 说明:csv写入流
 * <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class CSVWriter {
	/**
	 *空引用字符
	 */
	public static final char NO_QUOTE_CHARACTER = '\u0000';
	/**
	 * 空退出字符
	 */
	public static final char NO_ESCAPE_CHARACTER = '\u0000';
   /**
    * 默认分割字符
    */
	public static final char DEFAULT_SEPARATOR = ',';
	/**
	 * 默认应用字符
	 */
	public static final char DEFAULT_QUOTE_CHARACTER = NO_QUOTE_CHARACTER;
	
	/**
	 * 默认“字符
	 */
	public static final char DEFAULT_ESCAPE_CHARACTER = '"';
   /**
    * 默认换行字符
    */
	public static final String DEFAULT_LINE_END = "\n";
	/**
	 * 写入流
	 */
	private final Writer rawWriter;
   /**
    * 打印流
    */
	private final PrintWriter pw;
   /**
    * 分隔符
    */
	private final char separator;
   /**
    * 引用符
    */
	private final char quotechar;
   /**
    * 退出符
    */
	private final char escapechar;
   /**
    * 行结束
    */
	private final String lineEnd;
   /**
    * 
   * Constructor Method  
   * @update 2012-9-24 下午12:14:22  
   * @param writer 写入流
    */
	public CSVWriter(final Writer writer) {
		this(writer, DEFAULT_SEPARATOR);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-24 下午12:14:27  
   * @param writer 写入流
   * @param separator 分隔符
    */
	public CSVWriter(final Writer writer, final char separator) {
		this(writer, separator, DEFAULT_QUOTE_CHARACTER);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-24 下午12:14:33  
   * @param writer 写入流
   * @param separator 分隔符
   * @param quotechar 应用符
    */
	public CSVWriter(final Writer writer, final char separator, final char quotechar) {
		this(writer, separator, quotechar, DEFAULT_ESCAPE_CHARACTER);
	} 
   /**
    * 
   * Constructor Method  
   * @update 2012-9-24 下午12:14:40  
   * @param writer 写入流
   * @param separator 分隔符
   * @param quotechar 引用符
   * @param escapechar 退出符
    */
	public CSVWriter(final Writer writer, final char separator, final char quotechar,
			final char escapechar) {
		this(writer, separator, quotechar, escapechar, DEFAULT_LINE_END);
	}
  /**
   * 
  * Constructor Method  
  * @update 2012-9-24 下午12:14:49  
  * @param writer 吸入流
  * @param separator 分隔符
  * @param quotechar 引用符
  * @param lineEnd 行结束符
   */
	public CSVWriter(final Writer writer, final char separator, final char quotechar,
			final String lineEnd) {
		this(writer, separator, quotechar, DEFAULT_ESCAPE_CHARACTER, lineEnd);
	}
   /**
    * 
   * Constructor Method  
   * @update 2012-9-24 下午12:15:24  
   * @param writer 写入流
   * @param separator 分割符
   * @param quotechar 引用符
   * @param escapechar 退出符
   * @param lineEnd  行结束符
    */
	public CSVWriter(final Writer writer, final char separator, final char quotechar,
			final char escapechar, final String lineEnd) {
		this.rawWriter = writer;
		this.pw = new PrintWriter(writer);
		this.separator = separator;
		this.quotechar = quotechar;
		this.escapechar = escapechar;
		this.lineEnd = lineEnd;
	}
   /**
   *  说明:写入所有
   * @param allLines 所有行
   * @update 2012-9-24 下午12:16:16
    */
	public void writeAll(final List<?> allLines) {
		for (final Iterator<?> iter = allLines.iterator(); iter.hasNext();) {
			final String[] nextLine = (String[]) iter.next();
			writeNext(nextLine);
		}
	}
	/**
	 * 说明:写入行
	* @param nextLine 字符行
	* @update 2012-9-24 下午12:17:09
	 */
 	public void writeNext(final String[] nextLine) {
		if (nextLine == null) {
			return;
		}

		final StringBuilder sb = new StringBuilder();
		for (int i = 0; i < nextLine.length; i++) {
			if (i != 0) {//i不为0第一行,后续行前加分隔符
				sb.append(separator);
			}
			final String nextElement = nextLine[i];//获得字符行
			if (nextElement == null) {//如果行为空
				continue;
			}
			if (quotechar != NO_QUOTE_CHARACTER) {//如果引用符不是空引用
				sb.append(quotechar); //加入引用符
			}
			for (int j = 0; j < nextElement.length(); j++) {//行字符循环
				final char nextChar = nextElement.charAt(j);
				//字符不为弄退出符并字符为应用符
				if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {
					sb.append(escapechar).append(nextChar);//退出符再加
				} else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == escapechar) {
					sb.append(escapechar).append(nextChar);
				} else {
					sb.append(nextChar);
				}
			}
			if (quotechar != NO_QUOTE_CHARACTER) {
				sb.append(quotechar);
			}
		}

		sb.append(lineEnd);
		pw.write(sb.toString());
	}
   /**
    * 
   * 说明：冲刷流缓存
   * @throws IOException
   * @update 2012-10-2 下午2:21:59
    */
	public void flush() throws IOException {
		pw.flush();
	}
   /**
    * 
   * 说明：关闭流
   * @throws IOException
   * @update 2012-10-2 下午2:22:46
    */
	public void close() throws IOException {
		pw.flush();
		pw.close();
		rawWriter.close();
	}
}
